package it.fantacalcio.struts.action.amministrazione.role;

import it.fantacalcio.model.Role;
import it.fantacalcio.model.User;
import it.fantacalcio.spring.service.MyServiceFactory;
import it.fantacalcio.struts.form.RoleForm;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class PrepareListRoleAction extends Action {
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		User userInSession = (User) request.getSession().getAttribute("userInSession");
		if(userInSession==null || userInSession.getAttivo().equals("NO") || !userInSession.getRole().getCodRole().equals("ADMIN")){
			return mapping.findForward("noninsession");
		}
		
		RoleForm roleForm = (RoleForm) form;
		List<Role> listaRole = new ArrayList<Role>();
		try{
			Role role = new Role();
			BeanUtils.copyProperties(role, roleForm);
			listaRole = MyServiceFactory.getRoleService().findByExample(role);
		}catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("error");
		}
		request.setAttribute("listaRole", listaRole);
		return mapping.findForward("lista");
	}

}
